package com.ssm.controller;

import com.mysql.cj.Session;
import com.ssm.model.*;
import com.ssm.service.BookService;
import com.ssm.service.DeliveryService;
import com.ssm.service.OrderService;
import com.ssm.service.Order_bookService;
import com.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.UUID;
import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.*;


@Controller

public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private DeliveryService deliveryService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private Order_bookService order_bookService;

    @Autowired
    private BookService bookService;

    @RequestMapping("/sss")
    public  String selectUser(Model model, HttpServletRequest request) throws Exception {



        List<User> users = userService.selectAllUser();
        request.setAttribute("users", users);
        return "user";
    }

    @RequestMapping("/touserlogin") //跳转用户登陆页面
    public String to_user_login()
    {

        return "user_login";
    }

    @RequestMapping("/userlogin") //登陆控制器
    public String user_login(String username, String password, HttpSession session)
    {
        User user = userService.selectUserByUsername(username, password);

        if (user != null)
        {
            session.setAttribute("username", username);
            Map<String, Integer> map = new HashMap<String, Integer>();
            session.setAttribute("map", map);
            return "redirect:/tohome";
        }
        else
        {
            return "user_login";
        }

    }


    @RequestMapping("/touserregister") //跳转用户注册页面
    public String touserregister()
    {
        return "user_register";
    }

    @RequestMapping(value = "/userregister", method = RequestMethod.POST)
    public String usergrisster(User user)
    {
        userService.addUser(user);
        return "user_login";
    }

    @RequestMapping("/tousermanage")
    public String tousermanage(Model model)
    {
        List<User> users = userService.selectAllUser();
        model.addAttribute("users", users);
        return "user_manage";
    }

    @RequestMapping("/topay")
    public String topay(Model model, HttpSession session)
    {

        String username = (String)session.getAttribute("username");
        User user = userService.selectUserByusername(username);
        double total = (double)session.getAttribute("total");
        List<Cart> carts = (List<Cart>)session.getAttribute("carts");
        model.addAttribute("total", total);
        model.addAttribute("carts", carts);
        model.addAttribute("user", user);
        return "pay";
    }

    @RequestMapping("/pay")
    public String pay(Model model, HttpSession session)
    {
        Timestamp timestamp = new Timestamp(new Date().getTime());
        String username = (String)session.getAttribute("username");
        User user = userService.selectUserByusername(username);
        String preUuid = UUID.randomUUID().toString().replace("-", "");
        List<Cart> carts = (List<Cart>)session.getAttribute("carts");
        double total = (double)session.getAttribute("total");
        Order order = new Order(preUuid, username, total, timestamp, false, false);
        orderService.addOrder(order);

        for (Cart cart:carts)
        {


                String book_id = cart.getBook_id();
                int quantity = cart.getQuantity();
                double price = cart.getPrice();
                Order_book order_book = new Order_book(preUuid, book_id, quantity, price);
                order_bookService.addOrder_book(order_book);
                bookService.decreaseQuantity(book_id, quantity);

        }
        Delivery delivery = new Delivery(preUuid, "仓库", user.getAddress(), false);
        deliveryService.addDelivery(delivery);
        Map<String, Integer> map= (Map<String, Integer>)session.getAttribute("map");
        for (Map.Entry<String, Integer> entry : map.entrySet())
        {
            if (entry.getValue() > 0)
            {
                map.put(entry.getKey(), 0);
            }
        }
        session.setAttribute("map", map);
        return "redirect:/tohome";
    }

    @RequestMapping("/topayone")
    public String topayone(String book_id, Model model, HttpSession session)
    {
        Book book = bookService.selectBookByBook_id(book_id);
        String username = (String)session.getAttribute("username");
        User user = userService.selectUserByusername(username);
        Cart cart = new Cart(book.getBook_id(), book.getQuantity(), book.getPrice(), book.getName());
        model.addAttribute("cart", cart);
        model.addAttribute("user", user);
        session.setAttribute("book_id", book_id);
        return "payone";
    }

    @RequestMapping("/payone")
    public String payone(HttpSession session)
    {
        String book_id = (String)session.getAttribute("book_id");
        Timestamp timestamp = new Timestamp(new Date().getTime());
        Book book = bookService.selectBookByBook_id(book_id);
        String username = (String)session.getAttribute("username");
        User user = userService.selectUserByusername(username);
        String preUuid = UUID.randomUUID().toString().replace("-", "");
        Order order = new Order(preUuid, username, book.getPrice(), timestamp, false, false);
        orderService.addOrder(order);
        Order_book order_book = new Order_book(preUuid, book_id, 1, book.getPrice());
        Delivery delivery = new Delivery(preUuid, "仓库", user.getAddress(), false);
        order_bookService.addOrder_book(order_book);
        bookService.decreaseQuantity(book_id, 1);
        deliveryService.addDelivery(delivery);
        return "redirect:/tohome";
    }

    @RequestMapping("/tomymanage")
    public String tomymanage(Model model, HttpSession session)
    {
        String username = (String)session.getAttribute("username");
        User user = userService.selectUserByusername(username);
        model.addAttribute("user", user);
        return "mymanage";
    }

    @RequestMapping(value = "/mymanage", method = RequestMethod.POST)
    public String mymamage(User user, Model model, HttpSession session)
    {
        user.setUsername((String)session.getAttribute("username"));
        userService.updateUser(user);
        return "redirect:/touserlogin";
    }
    @RequestMapping("/myorder")
    public String myorder(Model model, HttpSession session)
    {
        String username = (String)session.getAttribute("username");
        List<Order> orders = orderService.selectOrderByUsername(username);
        model.addAttribute("orders", orders);
        return "myorder";
    }
}
